#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//顺序查找
typedef int ElemType;

typedef struct
{
    ElemType * data;
    int length;
}SSTable;

void st_init(SSTable &ST, int len)
{
    ST.length=len+1;     //第一个位置为哨兵位
    ST.data=(ElemType*)malloc(sizeof(ElemType)*(len+1));  //开辟数组空间
    //填入随机数
    srand(time(NULL));
    for(int i=1; i<ST.length; i++)
    {
        ST.data[i]=rand()%100;   //0~99的随机数
    }
}

//打印数组
void st_print(SSTable &ST)
{
    for(int i=1; i<ST.length; i++)
    {
        printf("%3d",*(ST.data+i));
    }
    printf("\n");
}


//查找元素下标
int search_seq(SSTable &ST,ElemType key)
{
    ST.data[0]=key;
    int i=0;
    for(i=ST.length-1; ST.data[i]!=key; i--);
    return i;
}
int main() {
    SSTable ST;
    st_init(ST,10);
    st_print(ST);
    printf("please input search key:\n");
    ElemType key;
    scanf("%d",&key);
    int pos=search_seq(ST,key);
    if(pos)
    {
        printf("the key pos is: %d\n",pos);
    }
    else
    {
        printf("the key is not exist\n");
    }
    return 0;
}
